package cn.com.dhcc.ddi.task.controller;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.modulehandler.valuebean.LikeValue;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.pubmodule.sysmgr.usermgr.service.CmUserMgrService;
import cn.com.dhcc.app.core.pubmodule.sysmgr.usermgr.vo.UserInfoVo;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode;
import cn.com.dhcc.app.core.util.JsonUtil;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.app.pub.core.consts.DdiProperties;
import cn.com.dhcc.app.pub.core.handler.IAnalyzeCallBack;
import cn.com.dhcc.app.pub.core.tag.staticcode.StaticCode;
import cn.com.dhcc.app.pub.core.tag.staticcode.StaticCode.TASK_STATUS;
import cn.com.dhcc.app.pub.core.util.FileUtil;
import cn.com.dhcc.app.pub.core.util.FileZip;
import cn.com.dhcc.ddi.datamapping.service.DataMappingService;
import cn.com.dhcc.ddi.datamapping.vo.DataMapping;
import cn.com.dhcc.ddi.datasource.service.DataSourceService;
import cn.com.dhcc.ddi.datasource.service.TableColService;
import cn.com.dhcc.ddi.datasource.service.TableInfoService;
import cn.com.dhcc.ddi.datasource.vo.TableColVo;
import cn.com.dhcc.ddi.datasource.vo.TableInfoVo;
import cn.com.dhcc.ddi.node.service.DataCategoryService;
import cn.com.dhcc.ddi.node.service.DomainInfoService;
import cn.com.dhcc.ddi.node.vo.DataCategory;
import cn.com.dhcc.ddi.node.vo.DomainInfo;
import cn.com.dhcc.ddi.task.service.*;
import cn.com.dhcc.ddi.task.service.load.DataFileHandler;
import cn.com.dhcc.ddi.task.vo.*;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.*;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.*;
/**
 * 
 * @类: TaskController
 * @描述: task controller
 * @作者: 张洪涛
 * @日期: 2016年11月11日 下午3:37:04
 * @版权所有: 东华软件
 * @版本:1.0
 */
@Controller
@RequestMapping("/task")
public class TaskController extends BaseController{
	@Autowired
	TaskService taskService;
	@Autowired
	DataMappingService dataMappingService;
	@Autowired
	DataSourceService dataSourceService;
	@Autowired
	TableInfoService tableInfoService;
	@Autowired
	OntimeService ontimeService;
	@Autowired
	PeriodService periodSerivce;
	@Autowired
	TaskTabService taskTabService;
	@Autowired
	TableColService tableColService;
	@Autowired
	DomainInfoService domainInfoService; 
	@Autowired
	DataCategoryService dataCategoryService;
	@Autowired
    TaskExecuteLogService taskExecuteLogService;
	@Autowired
	CmUserMgrService userMgrService;
	@Autowired
	SettlementService settService;
	@Autowired
    TaskFilterService taskFilterService;

	public static final Long MAX_NUM = 65534l;
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	
	@RequestMapping(value = "/task_list")
	public String taskList(Model model, Pager pager, String task, String pagerListType, String listType, Integer offSet1, Integer offSet2,HttpSession httpSession) {
		WhereCondition periodSearchData = new WhereCondition();
		WhereCondition ontimeSearchData = new WhereCondition();
		WhereCondition historySearchData = new WhereCondition();
		Pager ontimePager = new Pager(0, 10);
		Pager periodPager = new Pager(0, 10);
		Pager historyPager = new Pager(0, 10);
		int ontimeOffSet = 0;
		int periodOffSet = 0;
		int historyOffSet = 0;
		try {
			historySearchData.where().eq("t1.status", "4");
			Boolean isAdmin = BaseController.getCurrentUserInfo(httpSession).isSuperAdmin();

			if(listType == null && pagerListType != null) {
				listType = pagerListType;
			}

			if(listType != null && offSet1 != null && offSet2 != null && pagerListType != null) {	
				if(pagerListType.equals("0")) {//周期性任务列表
					periodPager = pager;
					ontimePager = new Pager(offSet1, 10);
					historyPager = new Pager(offSet2, 10);
					periodOffSet = pager.getPg().getOffset();
					
				} else if(pagerListType.equals("1")) {//一次性任务列表
					ontimePager = pager;
					periodPager = new Pager(offSet1, 10);
					historyPager = new Pager(offSet2, 10);
				} else if(pagerListType.equals("2")) {//历史任务列表
					historyPager = pager;
					periodPager = new Pager(offSet1, 10);
					ontimePager = new Pager(offSet2, 10);
					historyOffSet = pager.getPg().getOffset();
				}
			}
			

			
			
			//按任务名称搜索
			if (StrUtil.isNotBlank(task)){
				historySearchData.and().like("task_name", LikeValue.roundLike(task));
				periodSearchData.and().like("t1.task_name", LikeValue.roundLike(task));
				ontimeSearchData.and().like("t1.task_name", LikeValue.roundLike(task));
				/*if(searchData.getWhereCondition().isEmpty()) {
					searchData.where1Eq1();
				}
				
				searchData.and().like("name", LikeValue.roundLike(node));*/
			}
			String userId = BaseController.getCurrentUserInfo(httpSession).getId();
			UserInfoVo currentUser = userMgrService.getUserById(userId);
			if(!isAdmin){
				if(currentUser != null){
					String nodeIdsStr = currentUser.getExt2();
					WhereCondition dmParameter = new WhereCondition();
					dmParameter.where();
					if(StrUtil.isNotBank(nodeIdsStr)){
						String[] nodeIds = nodeIdsStr.split(",");
						dmParameter.in("t2.node_id", nodeIds);
						dmParameter.and().in("t3.node_id", nodeIds);
					}else{
						dmParameter.eq("1", "2");
					}
					List<String> dmids = dataMappingService.getDmid(dmParameter);
					//过滤用户数据
					if (dmids!=null && dmids.size()>0){
						ontimeSearchData.and().in("t1.mp_id",dmids);
						periodSearchData.and().in("t1.mp_id",dmids);
						historySearchData.and().in("t1.mp_id",dmids);
					}else {
						ontimeSearchData.and().eq("1","2");
						periodSearchData.and().eq("1","2");
						historySearchData.and().eq("1","2");
					}
				}
			}
			historySearchData.groupBy("t1.id,t1.task_name,t1.task_freq_type,t1.mtime");
			historySearchData.orderBy("t1.mtime desc");
			//当前任务
			periodSearchData.groupBy("t1.id,t1.task_name,t1.task_freq_type,t1.mtime,t3.freq,t3.interval_vallue,t3.execute_time,t1.status");
			ontimeSearchData.orderBy("status desc, t1.mtime desc");
			periodSearchData.orderBy("status desc, t1.mtime desc");
			PagerData<Task> historyTasks = taskService.getHistoryList(historyPager, historySearchData);
			PagerData<Task> periodtasks = taskService.getPeriodPagerList(periodPager, periodSearchData);
			PagerData<Task> oneTimetasks = taskService.getOnTimePagerList(ontimePager, ontimeSearchData);
			model.addAttribute("historyList", historyTasks.getDatas());
			model.addAttribute("periodList", periodtasks.getDatas());
			model.addAttribute("onTimeList", oneTimetasks.getDatas());
			model.addAttribute("periodTotal", periodtasks.getTotal());
			model.addAttribute("ontimeTotal", oneTimetasks.getTotal());
			model.addAttribute("historyTotal", historyTasks.getTotal());
			model.addAttribute("listType", listType);
			model.addAttribute("ontimeOffSet", ontimeOffSet);
			model.addAttribute("periodOffSet", periodOffSet);
			model.addAttribute("historyOffSet", historyOffSet);
			model.addAttribute("currentUser", currentUser);

			return "task/task_list";
		} catch (ServiceException e) {
			logger.error("获取任务列表失败", e);
		}

		return toAlertPage(model, "操作失败");
	}
	
	/**
	 * 获取任务
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/task_get")
	public String get(Model model, String id) {
		try {
			Task vo = taskService.get(id);
			if (vo != null) {
				DataMapping dm = dataMappingService.getMappingById(vo.getMpId());
				model.addAttribute("dm", dm);
				
				DataCategory dataCatagory = dataCategoryService.get(vo.getDateCategoryId());
				model.addAttribute("dc", dataCatagory);
				
				if ("1".equals(vo.getTaskFreqType())) {
					TaskOntime ontime = ontimeService.getOntimeTaskById(id);
					model.addAttribute("ontime", ontime);
				} else {
					TaskPeriod period = periodSerivce.getPeriodTaskById(id);
					if ("1".equals(period.getFreq())) {
						period.setExecuteTime("");
					}
					model.addAttribute("period", period);
				}
				if(DataMapping.MP_TYPE.TABLE_TO_TABLE.getCode().equals(dm.getMappingType())){
					List<TaskTab> tables = taskTabService.getTaskTabByTaskId(id);

					//查询是否有增量字段
					for(int i = 0; i < tables.size(); i++) {
						TaskTab tab = tables.get(i);
						TableColVo col = tableColService.getIncrementalColumn(tab.getTableId());

						if(col == null) {
							tab.setTableType("0");
						} else {
							tab.setTableType("1");
						}

						tables.set(i, tab);
					}
					model.addAttribute("tables", tables);
				}else if(DataMapping.MP_TYPE.FILE_TO_FILE.getCode().equals(dm.getMappingType())){
					TaskFilter filter = taskFilterService.searchTaskFilterByTaskId(id);
					model.addAttribute("filter", filter);
				}

				model.addAttribute("vo", vo);
			}

		} catch (ServiceException e) {
			logger.error(e);
			return toAlertPage(model, "查看发送任务失败");
		}
		return "task/task_get";
	}
	
	@RequestMapping(value = "/task_add")
	public String taskAdd(Model model, String taskType,HttpSession httpSession) {
		try {
			if(taskType == null) {
				taskType = "2";
			}
			Boolean isAdmin = BaseController.getCurrentUserInfo(httpSession).isSuperAdmin();
			String userId = BaseController.getCurrentUserInfo(httpSession).getId();
			UserInfoVo currentUser = userMgrService.getUserById(userId);
			WhereCondition searchParameter = new WhereCondition();
			if(!isAdmin){
				if(currentUser != null){
					String nodeIdsStr = currentUser.getExt2();
					if(StrUtil.isNotBank(nodeIdsStr)){
						String[] nodeIds = nodeIdsStr.split(",");
						searchParameter.andIn("t2.node_id", nodeIds);
						searchParameter.andIn("t3.node_id", nodeIds);
					}else{
						searchParameter.and().eq("1", "2");
					}
				}
			}
			searchParameter.orderBy("t1.mtime desc");
			List<DataMapping> dataMappings = dataMappingService.getMappingList(searchParameter);
			List<DomainInfo> domainInfos = domainInfoService.getDomain();
			model.addAttribute("ds_mappings", dataMappings);
			model.addAttribute("domains", domainInfos);
			model.addAttribute("taskType", taskType);
			return "task/task_add";
		} catch (ServiceException e) {
			logger.error("获取任务列表失败", e);
		}

		return toAlertPage(model, "操作失败");
	}
	
	
	
	@RequestMapping(value = "/add_task", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addTask(Model model, Task task, Tables tables, TaskOntime ontime,
			TaskPeriod period, TaskExecuteTime executeTime,TaskFilter taskFilter) {
		try {
			task.setId(UUIDGenerator.getUUID());
			DataMapping dm = dataMappingService.get(task.getMpId());
			task.setMtime(new Date());
			if(ontime.getExecuteType().equals(StaticCode.TaskExecuteTypeEnum.IMME.getCode())) {
				task.setStatus(TASK_STATUS.WAIT.getCode());
			} else {
				task.setStatus(TASK_STATUS.STOP.getCode());
			}
			taskService.insertOrUpdateTask(task);
			if (task.getTaskFreqType().equals(StaticCode.TASK_FREQ_TYPE.ONETIME.getCode())) {
				ontime.setId(UUIDGenerator.getUUID());
				ontime.setTaskId(task.getId());
				ontime.setMtime(new Date());
				ontime.setExecuteTime(executeTime.getOntimeExecuteTime());
				ontimeService.insertOrUpdateOntimeTask(ontime);
			} else {
				period.setId(UUIDGenerator.getUUID());
				period.setTaskId(task.getId());
				period.setMtime(new Date());
				period.setExecuteTime(executeTime.getPeriodExecuteTime());
				period.setStartTime(executeTime.getOntimeExecuteTime());
				if (StaticCode.FREQ_TYPE.INTERVAL.getCode().equals(period.getFreq())) {
					period.setExecuteTime("");
				}else if(StaticCode.FREQ_TYPE.DAYLY.getCode().equals(period.getFreq())) {
					period.setIntervalVallue(null);
				}
				periodSerivce.insertOrUpdatePeriodTask(period);
			}
			if(DataMapping.MP_TYPE.TABLE_TO_TABLE.getCode().equals(dm.getMappingType())){
				if (tables.getTables() != null) {
					for (Iterator<TaskTab> iterator = tables.getTables().iterator(); iterator.hasNext();) {
						TaskTab table = iterator.next();
						table.setId(UUIDGenerator.getUUID());
						table.setMtime(new Date());
						table.setTaskId(task.getId());
						taskTabService.insertOrUpdateTaskTab(table);
					}
				}
			}else if(DataMapping.MP_TYPE.FILE_TO_FILE.getCode().equals(dm.getMappingType())){
                taskFilter.setId(UUIDGenerator.getUUID());
                taskFilter.setMtime(new Date());
                taskFilter.setTaskId(task.getId());
                taskFilterService.addTaskFilter(taskFilter);
			}


			return jsonSuccessResult("添加成功");
		} catch (Exception e) {
			logger.error(e);
			return jsonFailResult("添加失败");
		}
		
	}
	
	/**
	 * 修改任务
	 * 
	 * @param model
	 * @param id 任务id
	 * @return
	 */
	@RequestMapping(value = "/task_update")
	public String taskUpdate(Model model, String id, HttpSession httpSession, String taskType) {
		try {
			if(taskType == null) {
				taskType = "2";
			}
			
			Task vo = taskService.get(id);
			if (vo != null) {
				String editFlag = "1";
				Boolean isAdmin = BaseController.getCurrentUserInfo(httpSession).isSuperAdmin();
				String userId = BaseController.getCurrentUserInfo(httpSession).getId();
				UserInfoVo currentUser = userMgrService.getUserById(userId);
				WhereCondition searchParameter = new WhereCondition();
				if(!isAdmin){
					if(currentUser != null){
						String nodeIdsStr = currentUser.getExt2();
						if(StrUtil.isNotBank(nodeIdsStr)){
							String[] nodeIds = nodeIdsStr.split(",");
							searchParameter.andIn("t2.node_id", nodeIds);
							searchParameter.andIn("t3.node_id", nodeIds);
						}else{
							searchParameter.andEq("1", "2");
						}
					}
				}
				searchParameter.orderBy("t1.mtime desc");
				List<DataMapping> dataMappings = dataMappingService.getMappingList(searchParameter);
				model.addAttribute("ds_mappings", dataMappings);
				
				String dataCatagoryId = vo.getDateCategoryId();
				List<DomainInfo> domainInfos = domainInfoService.list();
				String domainId = dataCategoryService.getDomainId(dataCatagoryId);
				vo.setDomainId(domainId);
				List<DataCategory> dcList = dataCategoryService.getDcListByDomainId(domainId);
				model.addAttribute("domains", domainInfos);
				model.addAttribute("dataCatagories", dcList);
				
				if ("1".equals(vo.getTaskFreqType())) {
					TaskOntime ontime = ontimeService.getOntimeTaskById(id);
					model.addAttribute("ontime", ontime);
				} else {
					TaskPeriod period = periodSerivce.getPeriodTaskById(id);
					
					int logNum = taskExecuteLogService.getCount(id);
					if (logNum > 0) {
						editFlag = "0";
					}
					model.addAttribute("period", period);
				}
                String tablesJson =null;
                List<TaskTab> tables = null;
                TaskFilter filter = null;
                DataMapping dm = dataMappingService.get(vo.getMpId());
                model.addAttribute("dm", dm);
                if(DataMapping.MP_TYPE.TABLE_TO_TABLE.getCode().equals(dm.getMappingType())) {
                    tables = taskTabService.getTaskTabByTaskId(id);

                    //查询是否有增量字段并添加schema
                    for (int i = 0; i < tables.size(); i++) {
                        TaskTab tab = tables.get(i);
                        TableColVo col = tableColService.getIncrementalColumn(tab.getTableId());
                        TableInfoVo tableInfo = tableInfoService.getById(tab.getTableId());
                        tab.setSchema(tableInfo.getSchema());

                        if (col == null) {
                            tab.setTableType("0");
                        } else {
                            tab.setTableType("1");
                        }

                        tables.set(i, tab);
                    }
                    tablesJson = JsonUtil.toJson(tables);
                    model.addAttribute("tablesJson", tablesJson);
                    model.addAttribute("tableList", tables);
                }else if(DataMapping.MP_TYPE.FILE_TO_FILE.getCode().equals(dm.getMappingType())){
                    filter = taskFilterService.searchTaskFilterByTaskId(id);
                    model.addAttribute("tablesJson", "1");
                    model.addAttribute("tableList", "1");
                }
                model.addAttribute("filter", filter);
				model.addAttribute("editFlag", editFlag);
				model.addAttribute("vo", vo);
				model.addAttribute("taskType", taskType);
			}

		} catch (Exception e) {
			logger.error(e);
			return toAlertPage(model, "修改任务失败");
		}
		return "task/task_update";
	}
	
	/**
	 * 修改发送任务
	 * 
	 * @param model
	 * @param vo
	 * @param recvers
	 * @param tables
	 * @param ontime
	 * @param period
	 * @param ontimeId
	 * @param periodId
	 * @return
	 */
	@RequestMapping(value = "/update_task", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateTask(Model model, Task vo, Tables tables, TaskOntime ontime,
			TaskPeriod period, String ontimeId, String periodId, TaskExecuteTime executeTime,TaskFilter taskFilter) {
		try {
			//更新主任务
            DataMapping dm = dataMappingService.get(vo.getMpId());
			vo.setMtime(new Date());
			if(ontime.getExecuteType().equals(StaticCode.TaskExecuteTypeEnum.IMME.getCode())) {
				vo.setStatus(TASK_STATUS.WAIT.getCode());
			} else {
				vo.setStatus(TASK_STATUS.STOP.getCode());
			}
			taskService.insertOrUpdate(vo);
			
			//更新周期和一次性任务
			ontimeService.deleteByTaskId(vo.getId());// 删除一次性任务
			periodSerivce.deleteByTaskId(vo.getId());// 删除周期性任务
			if (vo.getTaskFreqType().equals(StaticCode.TASK_FREQ_TYPE.ONETIME.getCode())) {
				ontime.setId(UUIDGenerator.getUUID());
				ontime.setTaskId(vo.getId());
				ontime.setMtime(new Date());
				ontime.setExecuteTime(executeTime.getOntimeExecuteTime());
				ontimeService.insertOrUpdateOntimeTask(ontime);
			} else {
				period.setId(UUIDGenerator.getUUID());
				period.setTaskId(vo.getId());
				period.setMtime(new Date());
				period.setExecuteTime(executeTime.getPeriodExecuteTime());
				period.setStartTime(executeTime.getOntimeExecuteTime());
				if (StaticCode.FREQ_TYPE.INTERVAL.getCode().equals(period.getFreq())) {
					period.setExecuteTime("");
				}else if(StaticCode.FREQ_TYPE.DAYLY.getCode().equals(period.getFreq())) {
					period.setIntervalVallue(null);
				}
				periodSerivce.insertOrUpdatePeriodTask(period);
			}

			//删除旧表
			taskTabService.deleteByTaskId(vo.getId());
            taskFilterService.deleteTaskFilterByTaskId(vo.getId());


			if(DataMapping.MP_TYPE.TABLE_TO_TABLE.getCode().equals(dm.getMappingType())){
				//添加更新后的表
				if (tables.getTables() != null) {
					for (Iterator<TaskTab> iterator = tables.getTables().iterator(); iterator.hasNext();) {
						TaskTab table = iterator.next();
						table.setId(UUIDGenerator.getUUID());
						table.setMtime(new Date());
						table.setTaskId(vo.getId());
						taskTabService.insertOrUpdateTaskTab(table);
					}
				}
			}else if (DataMapping.MP_TYPE.FILE_TO_FILE.getCode().equals(dm.getMappingType())){
                taskFilter.setId(UUIDGenerator.getUUID());
                taskFilter.setMtime(new Date());
                taskFilter.setTaskId(vo.getId());
                taskFilterService.addTaskFilter(taskFilter);
			}

			return jsonSuccessResult("修改成功");
		} catch (Exception e) {
			logger.error(e);
			return jsonFailResult("修改失败");
		}
	}
	
	
	
	/**
	 * 动态加载业务域对应数据分类
	 * 
	 * @param httpSession
	 * @param model
	 * @param domainId 业务域id
	 * @return
	 */
	@RequestMapping(value = "/change_domain", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> changDataCatagory(HttpSession httpSession, Model model, String domainId) {
		try {
			List<Map<String, Object>> dcList = new ArrayList<Map<String, Object>>();
			List<DataCategory> dataCatagory = dataCategoryService.getUsableDcListByDomainId(domainId);
			
			for(DataCategory dc: dataCatagory) {
				Map<String, Object> st = new HashMap<String, Object>();
				st.put("id", dc.getId());
				st.put("name", dc.getName());
				
				dcList.add(st);
			}
			
			return jsonSuccessDataResult("获取数据分类", dcList);
		} catch (Exception e) {
			return jsonFailResult("获取数据分类");
		}
	}
	
	/**
	 * 修改结构化任务状态
	 * 
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/update_task_status", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateTaskStatus(Model model, String id, String status) {
		String msg = "";
		try {
			Task vo = taskService.get(id);
			vo.setStatus(status);
			taskService.update(vo);
			if (TASK_STATUS.STOP.getCode().equals(status)) {
				msg = "任务已暂停";
			} else {
				msg = "任务已开始执行";
			}
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "执行失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 动态加载数据源映射对应表
	 * 
	 * @param httpSession
	 * @param model
	 * @param domainId 业务域id
	 * @return
	 */
	@RequestMapping(value = "/change_table", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> changTable(HttpSession httpSession, Model model, String dsMappingId) {
		try {
			List<Map<String, Object>> tList = new ArrayList<Map<String, Object>>();
			DataMapping dataMapping = dataMappingService.getMappingById(dsMappingId);
			String sourceDsId = dataMapping.getSrcDsId();

			List<TableInfoVo> tableList = tableInfoService.getTableInfoByDsId(sourceDsId);
			
			//查询是否有增量字段
			for(int i = 0; i < tableList.size(); i++) {
				TableInfoVo tab = tableList.get(i);
				TableColVo col = tableColService.getIncrementalColumn(tab.getId());
				
				if(col == null) {
					tab.setTableType(CommonStaticCode.YESNO.NO.getDesc());
				} else {
					tab.setTableType(CommonStaticCode.YESNO.YES.getDesc());
				}
				
				tableList.set(i, tab);
			}
			
			/*for(TableInfoVo vo : tableList) {
				Map<String, Object> st = new HashMap<String, Object>();
				st.put("id", vo.getId());
				st.put("name", vo.getTableName());
				
				tList.add(st);
			}*/
			
			return jsonSuccessDataResult("获取数据源表", tableList);
		} catch (Exception e) {
			logger.error(e);
			return jsonFailResult("获取数据源表失败");
		}
	}
	
	/**
	 * 获取表字段信息
	 * 
	 * @param model
	 * @param tableId 表id
	 * @return
	 */
	@RequestMapping(value = "/get_table_col", method = RequestMethod.POST)
	public String getTableCol(Model model, String tableId) {
		try {
			List<TableColVo> tableCol = tableColService.getColsByTableId(tableId);
			model.addAttribute("cols", tableCol);
		} catch (Exception e) {
			logger.error(e);
		}
		
		return "task/col_table";
	}
	
	/**
	 * 任务名称唯一性
	 * 
	 * @param newValue
	 * @param oldValue
	 * @return
	 */
	@RequestMapping("/is_taskName_unique")
	@ResponseBody
	public String isTaskNameUnique(@RequestParam("taskName") String newValue, @RequestParam("oldValue") String oldValue) {
		try {
			if (newValue.equals(oldValue)) {
				return FORM_CHECK_SUCCESS;
			}
			boolean flag = taskService.isPropertyUnique("task_name", newValue, oldValue);
			if (flag) {
				return FORM_CHECK_SUCCESS;
			} else {
				return FORM_CHECK_FAIL;
			}
		} catch (Exception e) {
			return FORM_CHECK_FAIL;
		}
	}
	
	
	/**
	 * 删除任务 逻辑删除，移入历史
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete_task", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delete(Model model, String id) {
		String msg = "";
		try {
			Task vo = taskService.get(id);
			vo.setStatus(TASK_STATUS.OVER.getCode());
			vo.setMtime(new Date());
			taskService.update(vo);
			msg = "删除成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 彻底删除任务
	 * 
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/delete_history", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> deleteHistory(Model model, String id) {
		String msg = "";
		try {
			taskService.deleteHistory(id);
			msg = "删除成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 批量删除历史任务 彻底删除
	 * 
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/history_deletes", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchDeleteHistory(Model model, String[] ids) {
		String msg = "";
		try {
			for(String id: ids) {
				taskService.deleteHistory(id);
			}
			
			msg = "删除成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	
	
	/**
	 * 获取增量字段
	 * @param  httpSession
	 * @param  model
	 * @param  tableId 表id
	 * @return 增量字段
	 */
	@RequestMapping(value = "/get_ic", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getIncrementalColumn(HttpSession httpSession, Model model, String tableId) {
		try {
			TableColVo col = tableColService.getIncrementalColumn(tableId);
				
			return jsonSuccessDataResult("获取增量字段", col);
		} catch (Exception e) {
			logger.error(e);
			return jsonFailResult("获取增量字段出错");
		}
	}
	
	
	@RequestMapping(value = "/edit_where", method = RequestMethod.POST)
	public String whereEdit(Model model, String tableId, String oldWhere, String mappingId) {
		try {
			model.addAttribute("oldWhere", oldWhere);
			model.addAttribute("tableId", tableId);
			model.addAttribute("mappingId", mappingId);
		} catch (Exception e) {
			logger.error(e);
		}
		
		return "task/where_edit";
	}
	
	@RequestMapping(value = "/edit_interval", method = RequestMethod.POST)
	public String intervalEdit(Model model, String tableId, String oldStartValue, String oldEndValue, String taskType) {
		try {
			TableColVo col = tableColService.getIncrementalColumn(tableId);
			String colType = "";
			if(col != null) {
				colType = col.getColType().toLowerCase();
			}
			
			model.addAttribute("colType", colType);
			model.addAttribute("oldStartValue", oldStartValue);
			model.addAttribute("oldEndValue", oldEndValue);
			//model.addAttribute("isShow", hasStart);
			model.addAttribute("taskType", taskType);
			model.addAttribute("tableId", tableId);
		} catch (Exception e) {
			logger.error(e);
		}
		
		return "task/interval_edit";
	}
	
	@RequestMapping(value = "/select_table", method = RequestMethod.POST)
	public String tableSelect(Model model, String dsMappingId) {
		try {
			DataMapping dataMapping = dataMappingService.getMappingById(dsMappingId);
			String sourceDsId = dataMapping.getSrcDsId();

			List<TableInfoVo> tableList = tableInfoService.getTableInfoByDsId(sourceDsId);
			
			model.addAttribute("tableList", tableList);
		} catch (Exception e) {
			logger.error(e);
		}
		
		return "task/table_select";
	}
	
	@RequestMapping(value = "/task_log_list")
	public String taskLogList(HttpServletRequest request, Model model, Pager pager, String id,
			String listType, Integer checkedType) {
		try {
			WhereCondition searchData = new WhereCondition();
			//只取group_id为空的主任务日志
			searchData.and().isNull("t1.group_id");
			if (checkedType == null || checkedType == 0) {
				checkedType = 0;
				searchData.and().greaterThan("total", 0);
				// searchData.groupBy("t1.id,t1.task_id,t1.execute_begin_time,t1.execute_end_time,t1.status");
			}
			searchData.orderBy("t1.LINKSTARTTIME desc");
			Task vo = taskService.get(id);
			DataMapping dm = dataMappingService.getMappingById(vo.getMpId());
			model.addAttribute("dm", dm);
			PagerData<TaskExecuteLog> datas = taskExecuteLogService.getTaskLogPagerListByTaskId(pager, searchData, id);

			if (listType == null) {
				setUrlpara(request, "id=" + id + "&checkedType=" + checkedType);
			} else {
				setUrlpara(request, "id=" + id + "&listType=" + listType + "&checkedType=" + checkedType);
			}

			model.addAttribute(DATA_KEY, datas.getDatas());
			model.addAttribute("mpType",dm.getMappingType());
			setTotoal(model, datas.getTotal());
			return "task/task_log_list";
		} catch (ServiceException e) {
			logger.error("获取任务监控列表失败", e);
		}

		return toAlertPage(model, "操作失败");
	}
	
	@RequestMapping(value = "/sub_task_log_list", method = RequestMethod.POST)
	public String subTaskLogList(Model model, String id) {
		try {
			WhereCondition searchData = new WhereCondition();
			searchData.orderBy("t1.linktype asc");
			List<TaskExecuteLog> subtaskLogList = taskExecuteLogService.getSubTasksByGroupId(id, searchData);
			model.addAttribute("subtaskLogList", subtaskLogList);
			model.addAttribute("currentGroupId", id);
		} catch (Exception e) {
			logger.error(e);
		}
		
		return "task/subtask_list_log";
	}
	
	/**
	 * 批量删除任务 逻辑删除，移入历史
	 * 
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/task_deletes", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> batchDelete(Model model, String[] ids) {
		String msg = "";
		try {
			for(String id: ids) {
				Task vo = taskService.get(id);
				vo.setStatus(TASK_STATUS.OVER.getCode());
				vo.setMtime(new Date());
				taskService.update(vo);				
			}
			
			msg = "删除成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "删除失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
	@RequestMapping(value = "/task_executetask", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> executeTask(){
		String msg = "";
		try {
			taskService.executeTasks();
			msg = "执行成功";
			return jsonSuccessResult(msg);
		} catch (ServiceException e) {
			logger.error(e);
			msg = "执行失败";
			return jsonFailResult(msg);
		}
	}
	
	/**
	 * 发布条件测试
	 * 
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/table_sql_test")
	@ResponseBody
	public Map<String, Object> tabeleSqlTest(String dsMappingId, String tableId, String testSql) throws SQLException {
		String msg = "";
		try {
			boolean isOk = dataSourceService.tabeleSqlTest(dsMappingId, tableId, testSql);
			if (!isOk) {
				return jsonFailResult("发布条件错误");
			} else {
				return jsonSuccessResult("验证成功");
			}
		} catch (Exception e) {
			msg = "验证失败";
			logger.error(msg, e);
			return jsonFailResult("发布条件错误");
		}

	}

	/**
	 *重置异常任务
	 * @param vo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/reset_task", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> resetTask(Model model) {
		String msg = "";
		try {
			taskService.resetTasks();
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "执行失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}

	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/export_excel")
	public void exportExcel(final HttpServletRequest request, final HttpServletResponse response, String id) {
		logger.info("进入导出Excel，downloadData()...");
		File dirFile = new File(request.getRealPath("/temp_files"));
		File zipFile = new File(dirFile + File.separator + "数据文件" + ExportExcelService.format.format(new Date())
				+ ".zip");
		OutputStream out = null;
		FileInputStream inStream = null;
		try {
			TaskExecuteLog extractLog = taskExecuteLogService.getLinkLog(id, TaskExecuteLog.LINK_TYPE.EXTRACT.getCode());
			SAXParserFactory factory = SAXParserFactory.newInstance();
			final File uncFile = new File(copyAndUncomp(extractLog));
			final ExportExcelService service = CM.getBean(ExportExcelService.class);
			final List<String> fileNames = new ArrayList<String>();
			SAXParser parser = factory.newSAXParser();
			parser.parse(uncFile, new DataFileHandler(DdiProperties.APP_PARSE_MAXEXECNUM, new IAnalyzeCallBack() {
				@Override
				public void proccess(ChangeVo vo) throws NiceException {
					logger.info("开始解析 " + uncFile + " ,解析条数：" + DdiProperties.APP_PARSE_MAXEXECNUM);
					service.exportExcel(vo, request, fileNames);
					if (vo.isOver()) {
						uncFile.delete();
						logger.info("解析文件 " + uncFile + " 完成，删除。");
					}
				}
			}, UUIDGenerator.getUUID()));
			File[] srcfiles = new File[fileNames.size()];
			for (int i = 0, n = fileNames.size(); i < n; i++) {
				srcfiles[i] = new File(fileNames.get(i));
			}
			logger.info("开始压缩成ZIP文件" + zipFile);
			FileZip.ZipFiles(srcfiles, zipFile);
			inStream = new FileInputStream(zipFile);
			byte[] buf = new byte[4096];
			setResponseHeader(response);
			out = response.getOutputStream();
			int readLength;
			while (((readLength = inStream.read(buf)) != -1)) {
				out.write(buf, 0, readLength);
			}
			logger.info("zip文件压缩完成：" + zipFile);
			// 删除临时文件
			for (File srcFile : srcfiles) {
				srcFile.delete();
			}
		} catch (Exception e) {
			logger.error("下载数据文件失败", e);
		} finally {
			try {
				if (null != inStream) {
					inStream.close();
				}
				if (null != out) {
					out.close();
				}
				zipFile.delete();
			} catch (IOException e) {
				logger.error("关闭输入输出流发生异常", e);
			}
		}
	}
	public String copyAndUncomp(TaskExecuteLog extractLog) {
		// 先复制新的文件到指定目录中，操作复制后的数据文件，防止读取文件过程中错误，导致文件失效
		String inputPath = extractLog.getLinkDataFilePath();
		String outPath = DdiProperties.APP_UPLOAD_COPY_FILEPATH + File.separator + UUIDGenerator.getUUID();
		String outDataPath = outPath + File.separator + inputPath.substring(inputPath.lastIndexOf("\\") + 1);
		File inputFile = new File(inputPath);
		File outFile = new File(outDataPath);
		if(!outFile.exists()){
			outFile.mkdir();
		}
		logger.info("[copyAndUncomp]开始复制文件..." + inputPath);
		try {
			FileUtils.copyFile(inputFile, outFile);
		} catch (IOException e) {
			logger.error("[copyAndUncomp]copy " + inputFile + " 发生异常", e);
			return null;
		}
		logger.info("[copyAndUncomp]" + outFile + "文件复制完成...开始解压缩...");
		String unCompFile = FileUtil.unCompress(outFile, outPath);
		// 解压失败，则状态更改为“待执行”
		if (StringUtils.isEmpty(unCompFile)) {
			logger.error("[copyAndUncomp]执行解压文件 失败。");
			// 删除掉复制的文件
			if (outFile.exists()) {
				logger.error("[copyAndUncomp]解压文件失败，删除源文件。");
				outFile.delete();
			}
		}
		logger.info("[copyAndUncomp]" + outFile + "文件解压完成...解压后的文件为：" + unCompFile);
		return unCompFile;
	}
	/**
	 * 设置响应头
	 * */
	public void setResponseHeader(HttpServletResponse response) {
		try {
			Date date = new Date();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("数据文件", "UTF-8")
					+ ExportExcelService.format.format(date) + ".zip");
			response.addHeader("Pargam", "no-cache");
			response.addHeader("Cache-Control", "no-cache");
		} catch (UnsupportedEncodingException ex) {
			logger.error("urlencode发生异常", ex);
		}
	}

	/**
	 * 导出Excel前判断
	 *
	 * @param request
	 * @param id
	 * @return
	 */
	@SuppressWarnings("deprecation")
	@RequestMapping(value = "/before_export")
	@ResponseBody
	public Map<String, Object> beforeExport(HttpServletRequest request, String id) {
		if (DdiProperties.APP_PARSE_MAXEXECNUM > MAX_NUM) {
			logger.error("app.parse.maxexecnum配置参数最大值应不超过65534，现配置数为：" + DdiProperties.APP_PARSE_MAXEXECNUM + "，请修改配置文件参数。");
			return jsonFailResult("配置参数最大值应不超过65534");
		}
		File dirFile = new File(request.getRealPath("/temp_files"));
		if (!dirFile.exists()) {
			boolean isTrue = dirFile.mkdir();
			if (!isTrue) {
				logger.error("创建文件夹" + dirFile + "失败。");
				return jsonFailResult("temp_files文件夹创建失败");
			}
			logger.info("创建临时文件夹" + dirFile + " 成功");
		}
		return jsonSuccessResult("检查成功");
	}

	/**
	 * 结算
	 * @param model
	 * @param id
	 * @param response
	 */
	@RequestMapping(value = "/sett_tasks")
	@ResponseBody
	public Map<String, Object> settTasks(Model model, HttpServletResponse response) {
		String msg = "";
		try {
			settService.settlementStrucTasks();
			msg = "成功";
			return jsonSuccessResult(msg);
		} catch (Exception e) {
			msg = "执行失败";
			logger.error(msg, e);
			return jsonFailResult(msg);
		}
	}
}
